#include <stdlib.h>
#include <stdio.h>
#include <nds.h>
#include <xlibrary.h>

// common headers
#include "common.h"

// local headers
#include "plugin.h"

#include "sellist.h"

LLIST *choosePlugin(LLIST *llist) {
	if(!llist || ll_is_loop(llist)) return NULL;

	int lnum=ll_get_num_el(llist);

	LLIST *sel=llist;
	LLIST *first=ll_get_first_el(llist);
	LLIST *last=ll_get_last_el(llist);

	while(1) {
		swiWaitForVBlank();

		LLIST *cur=first;
		while(cur) {
			iprintf(sel==cur?"\n>%s":"\n %s", SP(cur)->name);
			cur=cur->next;
		}

		while(1) {
			if(keysDown() & KEY_UP) {
				sel=sel->prev;
				if(!sel) sel=last;
				break;
			}

			if(keysDown() & KEY_DOWN) {
				sel=sel->next;
				if(!sel) sel=first;
				break;
			}

			if(keysDown() & KEY_A) {
				while(!(keysUp() & KEY_A)) swiWaitForVBlank();
				swiWaitForVBlank();
				return sel;
			}

			swiWaitForVBlank();
		}

		iprintf("\x1b[%dA", lnum);
	}
}

